<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="波形名称" prop="fWaveName">
          <el-input
            v-model="queryParams.fWaveName"
            placeholder="请输入波形程序名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="添加日期">
              <el-date-picker
                v-model="queryParams.createTime"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:post:add']"
          >新增波形</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table ref="table" v-loading="loading" :data="waveformList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="fId" width="80"/>
        <el-table-column label="波形编号" align="center" prop="fWaveCode" width="120"/>
        <el-table-column label="波形名称" align="center" prop="fWaveName" width="150"/>
        <el-table-column label="支持尺寸" align="center" prop="fDimensions" width="120"/>
        <el-table-column label="IC 型号" align="center" prop="fIcChip" width="120"/>
        <el-table-column label="温度段（℃）" align="center" prop="fMaxNum"  width="150"/>
        <el-table-column label="波形效果" align="center" prop="fWaveOutcome" width="150">
        <template slot-scope="scope">
          <span v-for="item in JSON.parse(scope.row.fWaveOutcome)" style="margin-right: 2px;">{{ item }}</span>
        </template>
      </el-table-column>
        <el-table-column label="创建人" align="center" prop="fCreaterName" width="120"/>
        <el-table-column label="创建日期" align="center" prop="fCreateDate" width="180">
        </el-table-column>
        <el-table-column label="最后修改人" align="center" prop="fModifierName" width="120"/>
        <el-table-column label="最后修改日期" align="center" prop="fModifyDate" width="180"/>
        <el-table-column label="操作状态" align="center" prop="fOperationStatus" width="120"/> <!-- 新增的列 -->
        <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:post:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:post:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加或修改岗位对话框 -->
      <el-dialog :title="title" :visible.sync="open" append-to-body width="65%">
        <el-form ref="form" :model="form" :rules="rules" label-width="130px">
          <el-form-item label="波形名称" prop="fWaveName" required>
            <el-input v-model="form.fWaveName" placeholder="请输入波形名称"  />
          </el-form-item>
          <el-form-item label="支持尺寸" prop="fDimensions">
            <el-input v-model="form.fDimensions" placeholder="请输入支持尺寸" />
          </el-form-item>
          <el-form-item label="IC型号" prop="fIcChip">
            <el-input v-model="form.fIcChip" placeholder="请输入IC型号" />
          </el-form-item>
          <el-form-item label="最高温度（℃）" prop="fMaxNum">
            <el-input v-model="form.fMaxNum" placeholder="请输入最高温度" />
          </el-form-item>
          <el-form-item label="最低温度（℃）" prop="fMinNum">
            <el-input v-model="form.fMinNum" placeholder="请输入最低温度" />
          </el-form-item>
          <el-form-item label="波形效果" prop="fWaveOutcome">
            <el-checkbox-group   v-model="form.fWaveOutcome">
            <el-checkbox label="R值提升"></el-checkbox>
            <el-checkbox label="残影改善"></el-checkbox>
            <el-checkbox label="角白改善"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          
          <el-form-item label="效果描述" prop="fWaveDescribe">
            <el-input v-model="form.fWaveDescribe" type="textarea" placeholder="请输入效果描述" />
          </el-form-item>
          <el-form-item label="波形程序">
          <template slot-scope="scope">
            <FileUpload :value="form.fWaveUrl" :limit="1" :isShowTip="false"
              @input="uploadFile($event, scope.$index)" :fileType="['bin']" :disabled="isEdit == 1" ></FileUpload>
          </template>
        </el-form-item>
          <el-form-item label="是否启用" prop="fForbidStatus">
            <el-radio-group v-model="form.fForbidStatus">
              <el-radio
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注" prop="fRemark">
            <el-input v-model="form.fRemark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <div slot="footer" class="dialog-footer" style="margin-top: 10px;">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="handleConfirm">确 定</el-button>
          </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </template>
  
  <script>
  import { listWave, getWave, delWave, addWave, updateWave } from "@/api/basicData/waveform";
  
  export default {
    name: "Wave",
    dicts: ['sys_normal_disable'],
    data() {
      return {
  
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        waveformList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          // postCode: undefined,
          // postName: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          // postName: [
          //   { required: true, message: "岗位名称不能为空", trigger: "blur" }
          // ],
          // postCode: [
          //   { required: true, message: "岗位编码不能为空", trigger: "blur" }
          // ],
          // postSort: [
          //   { required: true, message: "岗位顺序不能为空", trigger: "blur" }
          // ]
        },
        selections:null
      };
    },
    created() {
      this.getList();
    },
    methods: {
      uploadFile(e,index) {
      console.log(e,index)
      // debugger
      this.$set(this.form, 'fWaveUrl', e);
            // this.form.fWaveUrl = e
            // this.waveformList[index].fPictureUrl = e
            // this.waveformList[index].fUrlState = this.waveformList[index].fPictureUrl?1:0
        },
        handleClose() {
            this.$emit('closeWave');
        },
        handleConfirm(){
            this.$emit('confirmWave',this.selections);
        },
      //导入文件
      handleChange(file, fileList) {
          this.fileList = fileList.slice(-3);
        },
      /** 查询波形列表 */
      getList() {
          this.loading = true;
        listWave(this.queryParams).then(response => {
          this.waveformList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
        },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          postId: undefined,
          postCode: undefined,
          postName: undefined,
          postSort: 0,
          status: "0",
          remark: undefined,
          fWaveOutcome:"",
          fileList: [],
          
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        if(selection.length>1){
            this.$modal.msgError("只能单选哦~");
            this.$refs.table.clearSelection()
            return
        }
        this.ids = selection.map(item => item.fId)
        this.single = selection.length!=1
        this.multiple = !selection.length
        this.selections = selection[0]
        console.log(selection)
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增波形";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const postId = row.fId || this.ids
        getWave(postId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改波形";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.postId != undefined) {
              updateWave(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWave(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const postIds = row.fId || this.ids;
        this.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function() {
          return delWave(postIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/wave/export', {
          ...this.queryParams
        }, `post_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  